/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.se16.printer;

/**
 *
 * @author danny
 */
public class Printer {
    private Ink ink;
    private Paper paper;
    
    private String content = "";// 纸张中内容
    private int posX = 0;// 当前横向位置，从0到charPerLine-1
    private int posY = 0;// 当前行数，从0到linePerPage-1
    private int posP = 1;// 当前页数
    
    public void print(String str) {
        System.out.println("使用 " + ink.getInk() + " 墨盒");
        System.out.println("在 "+paper.getSize()+" 纸上打印");
        System.out.println("----------------------------------");
        
        for (int i = 0; i < str.length(); i++) {
            putInChar(str.charAt(i));
        }
        
        // 补齐本页空行，并显示页码
        if (!(posX == 0 && posY == 0)) {
            int count = paper.getLinesPerPage() - posY;
            for (int i = 0; i < count; ++i) {
                content += Paper.NEW_LINE;
            }
            content += "== 第" + posP + "页 ==";
        }
        
        System.out.println(content);
    }


    /**
     * @return the ink
     */
    public Ink getInk() {
        return ink;
    }

    /**
     * @param ink the ink to set
     */
    public void setInk(Ink ink) {
        this.ink = ink;
    }

    /**
     * @return the paper
     */
    public Paper getPaper() {
        return paper;
    }

    /**
     * @param paper the paper to set
     */
    public void setPaper(Paper paper) {
        this.paper = paper;
    }

    private void putInChar(char c) {
        content += c;
        ++posX;
        // 判断是否换行
        if (posX == paper.getCharsPerLine()) {
            content += Paper.NEW_LINE;
            posX = 0;
            ++posY;
        }
        // 判断是否翻页
        if (posY == paper.getLinesPerPage()) {
            content += "== 第" + posP + "页 ==";
            content += Paper.NEW_LINE + Paper.NEW_LINE;
            posY = 0;
            ++posP;
        }
    }
}
